package tjs.study.notes.dotest.jdk.thread;

import java.util.concurrent.TimeUnit;

/**
 * Created by Nicky.Tang on 2021/11/16 11:01 下午
 *
 * @since 02.12.10
 */
public class DoTestOfThreadStop {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            while (true){
                //if(Thread.currentThread().interrupted()){////true-当前线程为中断状态，重置状态为false
                if(Thread.currentThread().isInterrupted()){//true-当前线程为中断状态，不重置状态
                    System.out.println("stop..."+Thread.currentThread().isInterrupted());
                    break;
                }
                System.out.println("running..."+Thread.currentThread().isInterrupted());
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("sleep error..."+Thread.currentThread().isInterrupted());
                    Thread.currentThread().interrupt();// sleep方法会重置中断状态为false，所以捕捉异常后再次中断，交由上层代码处理
                }
            }
        });
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        t1.interrupt();// 标识为中断状态
    }
}
